#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "camera.h"
#include "baidulpr.h"
#include "carsql.h"
#include "serialport.h"
#include "downfile.h"
#include "tts.h"
#include "login.h"
#include "dialog.h"
#include "qmyvideowidget.h"
#include <QGuiApplication>
#include <QScreen>
#include <QFile>
#include <QPixmap>
#include <QBuffer>
#include <QDebug>
#include <QSysInfo>
#include <QTime>
#include <QProcess>
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    easyInput *eInput =new easyInput(this);//输入法实例化
    eInput->installEventFilter(this);//安装事件过滤器

    play = new QMediaPlayer(this);
    //play->setNotifyInterval(1000);
    ui->Videowidget->setAspectRatioMode(Qt::IgnoreAspectRatio);
    ui->Videowidget->setMediaPlayer(play);
    play->setVideoOutput(ui->Videowidget);



    this->setFocus();
    this->row = -1;
    MyCom = new serialport;
    sqltimer = new QTimer;

    mysql = new carsql;

    tts = new TTS;
    tts->moveToThread(&TTSThread);

    carIDTTS = new downfile;
    carIDTTS->moveToThread(&downfileThread);

    Ocr = new BaiduLPR;
    Ocr->moveToThread(&OcrThread);
    /* 初始化摄像头线程 */
    Camera = new camera;
    Camera->moveToThread(&CameraThread);

   // connect(this,SIGNAL(CCTVON(bool)),Camera,SLOT(cameraProcess(bool)));
    connect(ui->pushButton,SIGNAL(clicked(bool)),Camera,SLOT(cameraProcess(bool)));
    /* 读取图片到label */
    connect(Camera,SIGNAL(readyImage(QImage)),this,SLOT(showImage(QImage)));
    /* 打开摄像头并保存图片到本地 */
    connect(MyCom,SIGNAL(openCamera()),Camera,SLOT(GetLicensePlate()));
    connect(MyCom,SIGNAL(saveImage(QString,int)),Camera,SLOT(SaveImage(QString,int)));
    /* 上传车牌并识别车牌 */
    connect(Camera,SIGNAL(CarIDReady(QString)),Ocr,SLOT(GetOCRResult(QString)));
    /* 上传人脸识别口罩检测 */
    connect(Camera,SIGNAL(FaceReady(QString)),Ocr,SLOT(GetMaskResult(QString)));
    /* 合成车牌音频 */
    connect(Ocr,SIGNAL(CarIDTTS(QString)),carIDTTS,SLOT(startDownload(QString)));

    /* 播放收费 */
    connect(mysql,SIGNAL(SeedPriceTTS(uint)),tts,SLOT(PriceTTS(uint)));
    /* 口罩语音 */
    connect(Ocr,SIGNAL(TTS_CMD(QString)),tts,SLOT(TTSPlay(QString)));

    connect(carIDTTS,SIGNAL(FileDownReady(QString)),tts,SLOT(TTSPlay(QString)));

    connect(this,SIGNAL(closeCamera()),Camera,SLOT(closecamera()));

    /* 发送测温度指令 */
    connect(mysql,SIGNAL(Seedimt(QString)),MyCom,SLOT(sendData(QString)));
    /* TTS播报 */
    connect(mysql,SIGNAL(SeedTTS(QString)),tts,SLOT(TTSPlay(QString)));
    /* 线程相关 */
    connect(&CameraThread,SIGNAL(finished()),Camera,SLOT(deleteLater()));
    connect(&CameraThread,SIGNAL(finished()),&CameraThread,SLOT(deleteLater()));
    connect(&OcrThread,SIGNAL(finished()),Ocr,SLOT(deleteLater()));
    connect(&OcrThread,SIGNAL(finished()),&OcrThread,SLOT(deleteLater()));
    connect(&downfileThread,SIGNAL(finished()),carIDTTS,SLOT(deleteLater()));
    connect(&downfileThread,SIGNAL(finished()),&downfileThread,SLOT(deleteLater()));
    connect(&TTSThread,SIGNAL(finished()),tts,SLOT(deleteLater()));
    connect(&TTSThread,SIGNAL(finished()),&TTSThread,SLOT(deleteLater()));


    connect(this,SIGNAL(sqlchaxuntest(QString)),mysql,SLOT(GetData(QString)));

    connect(this,SIGNAL(downfileUrl(QString)),carIDTTS,SLOT(startDownload(QString)));

    //connect(Ocr,SIGNAL(GetLRPData(QString)),this,SLOT(SetCarLabel(QString)));
    /* 解析信息 */
    connect(Ocr,SIGNAL(seedsqldata(QString)),mysql,SLOT(GetData(QString)));
    connect(MyCom,SIGNAL(sqltmp(QString)),mysql,SLOT(GetData(QString)));

    connect(sqltimer,SIGNAL(timeout()),this,SLOT(sqlshow()));
    connect(this,SIGNAL(sqltest()),mysql,SLOT(showalldata()));
    ui->time_show->setText(QDateTime::currentDateTime().toString("yyyy年MM月dd日"));
    ui->time_show_2->setText(QDateTime::currentDateTime().toString("dddd"));
    ui->time_show_3->setText(QDateTime::currentDateTime().toString("hh:mm:ss"));

    sqltimer->start(1000);
    /* 启动摄像头线程 */
    CameraThread.start();
    OcrThread.start();
    downfileThread.start();
    TTSThread.start();
}

MainWindow::~MainWindow()
{
     CameraThread.quit();
     CameraThread.wait();
     OcrThread.quit();
     OcrThread.wait();
     downfileThread.quit();
     downfileThread.wait();
     TTSThread.quit();
     TTSThread.wait();
     delete ui;
}

void MainWindow::showImage(const QImage &image)
{
    ui->label->setPixmap(QPixmap::fromImage(image));
}


void MainWindow::sqlshow()
{
    QSqlQueryModel *model = new QSqlQueryModel(this);
    if(QSysInfo::buildCpuArchitecture() == "arm"){
         model->setQuery("select * from carsql");
    }
    else{
        model->setQuery("select * from test");
    }


    model->setHeaderData(0,Qt::Horizontal,tr("序号"));
    model->setHeaderData(1,Qt::Horizontal,tr("车牌号"));
    model->setHeaderData(2,Qt::Horizontal,tr("入场时间"));
    model->setHeaderData(3,Qt::Horizontal,tr("出场时间"));
    model->setHeaderData(4,Qt::Horizontal,tr("口罩"));
    model->setHeaderData(5,Qt::Horizontal,tr("体温"));
    model->setHeaderData(6,Qt::Horizontal,tr("缴费情况"));
    ui->database_show->setModel(model);
    ui->database_show->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
    ui->database_show->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->database_show->horizontalHeader()->setHighlightSections(false);
    ui->time_show->setText(QDateTime::currentDateTime().toString("yyyy年MM月dd日"));
    ui->time_show_2->setText(QDateTime::currentDateTime().toString("dddd"));
    ui->time_show_3->setText(QDateTime::currentDateTime().toString("hh:mm:ss"));
}



void MainWindow::on_pushButton_16_clicked()
{
    login* loginui = new login;
    loginui->show();
}

void MainWindow::on_pushButton_17_clicked()
{
    this->close();
}

void MainWindow::on_pushButton_6_clicked()
{
    QString sys_name  = ui->lineEdit->text();
    QString sys_num   = ui->spinBox->text();
    QString sys_Price = ui->spinBox_2->text();
    if(!sys_name.isEmpty()){
        ui->label_10->setText(sys_name);
    }
    if(!sys_num.isEmpty()){
        ui->label_11->setText(sys_num);
    }
    if(!sys_Price.isEmpty()){
        mysql->SetPrice(sys_Price.toInt());
    }
    QMessageBox::information(this, tr("提示"),tr("同步成功!"), QMessageBox::Ok);
}


void MainWindow::on_database_show_clicked(const QModelIndex &index)
{
    QModelIndex index1 = ui->database_show->selectionModel()->currentIndex();
    int row1 = index1.row();
    this->row = row1;
}

void MainWindow::on_pushButton_7_clicked()
{
    if(this->row == -1){
         QMessageBox::information(this, tr("提示"),tr("没有选中数据!"), QMessageBox::Ok);
    }
    else{

        if(QMessageBox::Yes ==  QMessageBox::warning(this, tr("警告"),tr(QString("确定删除第%1条数据?").arg(this->row + 1).toStdString().c_str()),
                                QMessageBox::Yes,QMessageBox::No)){
            mysql->deletesqldata(this->row);
            this->row = -1;
        }
        else{
            this->row = -1;
        }
    }

}

void MainWindow::on_pushButton_8_clicked()
{
    if(QMessageBox::Yes == QMessageBox::warning(this,tr("警告"),tr("此操作会清空全部数据，请谨慎操作！"),QMessageBox::Yes,QMessageBox::No)){
        mysql->deletealldata();
    }

}

void MainWindow::on_pushButton_18_clicked()
{
    Dialog* inputdata = new Dialog;
    inputdata->setGeometry(0,480-301,800,301);
    inputdata->show();
}

void MainWindow::on_pushButton_9_clicked()
{
    QSqlQuery query;
    QString sql = QString("select * from carsql limit 1 offset %1").arg(this->row);

    QString carID ;
    QString inputdetetime;
    QString outputdatetime;
    QString tmp;
    QString mask;
    QString Price;
    qDebug()<<sql <<endl;
    if(query.exec(sql)){
        while(query.next()){
            carID = query.value(1).toString();
            inputdetetime = query.value(2).toString();
            outputdatetime = query.value(3).toString();
            mask = query.value(4).toString();
            tmp  = query.value(5).toString();
            Price = query.value(6).toString();
        }
    }
   qDebug()<<inputdetetime<<outputdatetime<<Price<<endl;
   Dialog* inputdata = new Dialog;
   inputdata->setGeometry(0,480-301,800,301);
   inputdata->show();
   inputdata->SetAllData(carID,inputdetetime,outputdatetime,mask,tmp,Price,this->row);

}

void MainWindow::on_pushButton_clicked(bool bl)
{
    if(bl){
        Camera->opencamera();
        ui->pushButton->setText("关闭监控");

    }
    else{
        ui->pushButton->setText("打开监控");
        emit this->closeCamera();
    }
}

void MainWindow::on_pushButton_2_clicked()
{
    system("ffmpeg -y -f v4l2 -s 800x480 -t 10 -i /dev/video2 /bishe/luxiang/out.avi");
}

void MainWindow::on_pushButton_3_clicked()
{
    play->setMedia(QUrl::fromLocalFile("/bishe/luxiang/out.avi"));
    play->play();
}

void MainWindow::on_pushButton_4_clicked()
{
    MyCom->sendData("CMD:Open_test");
    ui->label_18->setText("开");
    ui->label_18->setStyleSheet("color: rgb(115, 210, 22);");
}

void MainWindow::on_pushButton_5_clicked()
{
    MyCom->sendData("CMD:Close");
    ui->label_18->setText("关");
    ui->label_18->setStyleSheet("color: rgb(204, 0, 0);");
}
